package com.share.starter.sharding.rule;

import io.shardingsphere.api.algorithm.sharding.complex.ComplexKeysShardingAlgorithm;
import io.shardingsphere.api.config.rule.TableRuleConfiguration;
import io.shardingsphere.api.config.strategy.ComplexShardingStrategyConfiguration;
import org.springframework.util.StringUtils;

/**
 * @author syq
 * @data 2019/6/29
 */
public class BbtJdRuleConfig implements TableRuleConfig{
    private  String  logicTable;
    private String dbStrategyColumn;
    private Class dbColumnJavaClass;
    private String tabStrategyColumn;
    private Class tabColumnJavaClass;
    private int dbNum;
    private int tabNum;

    public BbtJdRuleConfig(String  logicTable,String dbStrategyColumn,Class dbColumnJavaClass, String tabStrategyColumn ,Class tabColumnJavaClass,int dbNum,int tabNum){
       if(StringUtils.isEmpty(logicTable)
               || StringUtils.isEmpty(dbColumnJavaClass)|| StringUtils.isEmpty(dbStrategyColumn)
               || StringUtils.isEmpty(tabColumnJavaClass)|| StringUtils.isEmpty(tabStrategyColumn)
               || (dbNum <= 0)|| ((tabNum <= 0))){
           throw new RuntimeException("数据库参数错误!!!");
       }
        this.logicTable = logicTable;

        this.dbStrategyColumn = dbStrategyColumn;
        this.dbColumnJavaClass=dbColumnJavaClass;

        this.tabStrategyColumn = tabStrategyColumn;
        this.tabColumnJavaClass = tabColumnJavaClass;

        this.dbNum = dbNum;
        this.tabNum = tabNum;
    }


    private String calcActualDataNodes() {
        /*ds${0..1}.bbt_jd_trade${0..1}*/
        return "ds"+calcNodes(this.dbNum)+"."+logicTable+calcNodes(tabNum);
    }

    private String calcNodes(int  num) {
        if(num == 1){
            return "0";
        }else{
            return "${0.."+(num-1)+"}";
        }
    }

    private ComplexKeysShardingAlgorithm calcComplexKeysShardingAlgorithm(Class<?> clazz,int dbNum,int tabNum) {
        if(String.class.equals(clazz)) {
            return new LongComplexKeysShardingAlgorithm(dbNum, tabNum);
        } else if(Long.class.equals(clazz) || long.class.equals(clazz)){
            return  new LongComplexKeysShardingAlgorithm(dbNum,tabNum);
        }else{
            throw new RuntimeException("数据库类型错误!!!");
        }
    }

    @Override
    public TableRuleConfiguration config() {
        // 配置分库分表数
        TableRuleConfiguration ruleConfig = new TableRuleConfiguration();
        ruleConfig.setLogicTable(this.logicTable);
        ruleConfig.setActualDataNodes(calcActualDataNodes());

        //配置分库规则
        ruleConfig.setDatabaseShardingStrategyConfig(new ComplexShardingStrategyConfiguration(this.dbStrategyColumn, this.calcComplexKeysShardingAlgorithm(dbColumnJavaClass,dbNum,tabNum)));

        //配置分表规则
        ruleConfig.setTableShardingStrategyConfig(new ComplexShardingStrategyConfiguration(this.tabStrategyColumn,this.calcComplexKeysShardingAlgorithm(tabColumnJavaClass,dbNum,tabNum)));

        return ruleConfig;
    }
}
